/**
 * @file qrcode_scan.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "lv_watch.h"

#if USE_LV_WATCH_QRCODE_SCAN != 0

#include <stdio.h>
#ifndef BUILD_IN_PC_SIMULATOR
#if USE_LV_WATCH_THIRD_LIB_QRCODE_SCAN != 0
#include "../../../lv_drivers/display/scaler.h"
#endif
#endif

/*********************
 *      DEFINES
 *********************/
#if USE_LV_WATCH_THIRD_LIB_QRCODE_SCAN == 0
#define YUV_DIRECT_CODE_ZXING 1
#endif
#define TEST_YUV_SIZE 460800
/**********************
 *      TYPEDEFS
 **********************/
typedef lv_fs_file_t ui_file_t;
/**********************
 *  STATIC PROTOTYPES
 **********************/
static void qrcode_scan_prepare_destory(lv_obj_t * activity_obj);
static void qrcode_scan_preview_task(lv_task_t * task);
static void qrcode_scan_decode_cb(void *string);

/**********************
 *  STATIC VARIABLES
 **********************/
static lv_img_dsc_t img_camera;
static lv_task_t * preview_task = NULL;
static char* yuvBuf=NULL;
#ifndef BUILD_IN_PC_SIMULATOR
#if USE_LV_WATCH_THIRD_LIB_QRCODE_SCAN != 0
static int count = 0;
#endif
#endif

/**********************
 *      MACROS
 **********************/

/**********************
 *   GLOBAL FUNCTIONS
***********************/
#ifdef BUILD_IN_PC_SIMULATOR
static void read_yuv_from_pc(char* yuvBuf_pc,const char* path)
{
    int ret = LV_FS_RES_OK;
    ui_file_t * pfile = NULL;
    uint32_t ret_len = 0;
#if 0
    char dir_path[5];
    strcpy(dir_path, "C:/*");
    lv_fs_dir_t dir;
    memset(&dir, 0, sizeof(dir));
    ret = lv_fs_dir_open(&dir, dir_path);
    printf("ret is %d in %s\n",ret,__FUNCTION__);
    if(ret == LV_FS_RES_OK) {
        printf("opendir OK handle:0x%x in save_preview_yuv_to_sd\n", *(uint32_t *)dir.dir_d);
        lv_fs_dir_close(&dir);
#endif
        pfile = lv_mem_alloc(sizeof(ui_file_t));
        memset(pfile,0,sizeof(ui_file_t));
        ret = lv_fs_open(pfile,path,LV_FS_MODE_RD);
        printf("ret is %d in lv_fs_open\n",ret);
        if(LV_FS_RES_OK != ret) {
            printf("%s is not exist!\n",path);
            ret = lv_fs_close(pfile);
            lv_mem_free(pfile);
            return;
        }
        printf("%s is exist!\n",path);
        ret = lv_fs_seek(pfile, 0);
        printf("ret is %d in lv_fs_seek\n",ret);
        ret = lv_fs_read(pfile, yuvBuf_pc, TEST_YUV_SIZE ,&ret_len);
        printf("ret is %d in lv_fs_read\n",ret);
        if(TEST_YUV_SIZE != ret_len)
        {
            printf("read %s is wrong!\n",path);
            lv_fs_close(pfile);
            lv_mem_free(pfile);
            return;
        }
        lv_fs_close(pfile);
        lv_mem_free(pfile);
#if 0
    } else {
        printf("opendir %s failed in save_preview_yuv_to_sd!\n", dir_path);
        return;
    }
#endif
}
#endif

/**
 * Create a qrcode scan objects
 * @param parent pointer to an object, it will be the parent of the new list
 * @return pointer to the created object.
 */
lv_obj_t * qrcode_scan_create(lv_obj_t * activity_obj)
{
    if(activity_obj == NULL) {
        lv_watch_activity_ext_t activity_ext;
        _lv_memset_00(&activity_ext, sizeof(lv_watch_activity_ext_t));
        activity_ext.actId = ACT_ID_QRCODE_SCAN;
        activity_ext.create = qrcode_scan_create;
        activity_ext.prepare_destory = qrcode_scan_prepare_destory;
        activity_obj = lv_watch_creat_activity_obj(&activity_ext);
        LV_ASSERT_MEM(activity_obj);
        if(activity_obj == NULL) return NULL;
    }

    lv_obj_t * obj = lv_watch_obj_create(activity_obj);
    LV_ASSERT_MEM(obj);
    if(obj == NULL) return NULL;
    lv_watch_obj_set_anim_mode(obj, LV_WATCH_ANIM_HOR_RIGHT_HIDE);

    lv_camera_obj_ext_t * ext = lv_obj_allocate_ext_attr(obj, sizeof(lv_camera_obj_ext_t));
    LV_ASSERT_MEM(ext);
    if(ext == NULL) return NULL;
    if(!WATCH_HAS_WALLPAPER) {
        lv_obj_t * img = lv_img_create(activity_obj, NULL);
        LV_ASSERT_MEM(img);
        if(img == NULL) return NULL;
        lv_img_set_src(img, watch_get_bg());
        lv_obj_set_size(img, LV_HOR_RES, LV_VER_RES);

        lv_obj_t * par = lv_obj_get_parent(obj);
        lv_obj_set_parent(obj, img);
        lv_obj_del(par);
    }
    ext->img = lv_obj_get_parent(obj);
    ext->timer = NULL;

    lv_img_cache_invalidate_src(NULL);
    img_camera.header.always_zero = 0;
    img_camera.header.w = lv_obj_get_width(lv_obj_get_parent(obj));
    img_camera.header.h = lv_obj_get_height(lv_obj_get_parent(obj));
    img_camera.data_size = img_camera.header.w * img_camera.header.h * LV_COLOR_SIZE / 8;
    img_camera.header.cf = LV_IMG_CF_TRUE_COLOR;
    img_camera.data = lv_mem_alloc(img_camera.header.w * img_camera.header.h * LV_COLOR_SIZE / 8);
    LV_ASSERT_MEM(img_camera.data);
    if(img_camera.data == NULL) return NULL;
    memset((void *)img_camera.data, 0, img_camera.header.w * img_camera.header.h * LV_COLOR_SIZE / 8);
    lv_img_set_src(ext->img, &img_camera);

    cam_start_preview_t cam_params;

    cam_params.width = 240;
    cam_params.height = 240;
    cam_params.sensor_id = 0;
    Hal_Camera_Start_Preview(&cam_params);

    int pre_w = LV_HOR_RES;
    int pre_h = LV_VER_RES;
#if USE_CRANE_CAMERA_FULL_SIZE
    Hal_Camera_Preview_Yuv_Size(&pre_w,&pre_h);
    if((pre_w * pre_h) > (640 * 480)) LV_ASSERT_MEM(activity_obj);
#endif
    yuvBuf = lv_mem_alloc(pre_w * pre_h * 3/2);
    LV_ASSERT_MEM(yuvBuf);
    if(yuvBuf == NULL) return NULL;
    memset((void *)yuvBuf, 0, pre_w * pre_h * 3/2);

    preview_task = lv_task_create(qrcode_scan_preview_task, 100, LV_TASK_PRIO_HIGH, NULL);

    return obj;
}

void qrcode_scan_create_event_cb(lv_obj_t * btn, lv_event_t event)
{
    (void)btn;

    if(LV_EVENT_CLICKED == event) {
        lv_watch_png_cache_all_free();

#ifndef BUILD_IN_PC_SIMULATOR
        lv_obj_t * obj = qrcode_scan_create(NULL);
        LV_ASSERT_MEM(obj);
        if(obj == NULL) return;
#else
        int result = 0;
        char * yuvBuf_pc = (char *)lv_mem_alloc(TEST_YUV_SIZE + 1);
        memset(yuvBuf_pc,0,(TEST_YUV_SIZE + 1));
        read_yuv_from_pc(yuvBuf_pc,"C:/test.yuv");
        char parseStr[4000];
        memset(parseStr,0,4000);
        result = qrcode_scan((char*)yuvBuf_pc,
                        pre_w,
                        pre_h / 2,
                        1,
                        parseStr,
                        4000);
        if(0 == result)
        {
            printf("parseStr is %s in %s\n",parseStr,__FUNCTION__);
        }
        printf("result is %u in %s\n",result,__FUNCTION__);
#endif
    }
}

/**********************
 *   STATIC FUNCTIONS
 **********************/
#ifndef BUILD_IN_PC_SIMULATOR
static void qrcode_scan_decode_create(lv_obj_t * obj,char *parseStr)
{
    lv_obj_t * watch_obj;
    lv_watch_get_child_obj(obj, lv_watch_obj_signal, &watch_obj);
    lv_obj_t * content = lv_cont_create(watch_obj, NULL);
    LV_ASSERT_MEM(content);
    if(content == NULL) return;
    lv_obj_add_style(content, LV_OBJ_PART_MAIN, &lv_watch_black_tight);
    lv_obj_set_size(content,  LV_HOR_RES, LV_VER_RES);
    lv_cont_set_layout(content, LV_LAYOUT_OFF);
    lv_watch_obj_add_element(content);

    lv_obj_t * label = lv_label_create(content, NULL);
    lv_obj_add_style(label, LV_OBJ_PART_MAIN, &lv_watch_font20_yellow);

    lv_label_set_align(label, LV_LABEL_ALIGN_CENTER);
    lv_obj_set_click(label, false);
    lv_label_set_text(label, parseStr);
    lv_label_set_long_mode(label, LV_LABEL_LONG_BREAK);
    lv_obj_set_width(label,220);
    lv_obj_align(label, content, LV_ALIGN_CENTER, 0, 0);

    lv_watch_obj_add_element(label);

    return;
}
#endif

static lv_obj_t * qrcode_scan_get_activity_obj(void)
{
    lv_obj_t * activity_obj = lv_watch_get_activity_obj(ACT_ID_QRCODE_SCAN);

    return activity_obj;
}

static lv_obj_t * qrcode_scan_get_obj(void)
{
    lv_obj_t * watch_obj = NULL;

    lv_obj_t * activity_obj = qrcode_scan_get_activity_obj();
    if(activity_obj) {
        lv_watch_get_child_obj(activity_obj, lv_watch_obj_signal, &watch_obj);
    }
    return watch_obj;
}

static lv_camera_obj_ext_t * qrcode_scan_get_ext(void)
{
    lv_camera_obj_ext_t  * ext = NULL;

    lv_obj_t * watch_obj = qrcode_scan_get_obj();
    if(watch_obj) {
        ext = lv_obj_get_ext_attr(watch_obj);
    }

    return ext;
}

#ifndef BUILD_IN_PC_SIMULATOR
static void qrcode_scan_decode_cb(void *string)
{
    char *str = (char *)string;
    lv_obj_t * cam_obj = lv_watch_get_activity_obj(ACT_ID_QRCODE_SCAN);
    if(str)
    {
        if(Hal_Camera_Get_On_Flag()) {
            Hal_Camera_Stop_Preview(0);
        }
        lv_task_del(preview_task);
        preview_task = NULL;
        printf("qrcode_scan:%s\n",str);
        qrcode_scan_decode_create(cam_obj,str);
        return;
    }
}
#endif

#if 0
static void save_preview_yuv_to_sd(char* preyuvBuf,const char* path,uint32_t write_size)
{
    int ret = LV_FS_RES_OK;
    ui_file_t * pfile = NULL;
    uint32_t ret_len = 0;
    char dir_path[6];
    strcpy(dir_path, "D:\\*");
    lv_fs_dir_t dir;
    memset(&dir, 0, sizeof(dir));
    ret = lv_fs_dir_open(&dir, dir_path);
    printf("ret is %d in %s\n",ret,__FUNCTION__);
    if(ret == LV_FS_RES_OK) {
        printf("opendir OK handle:0x%x in save_preview_yuv_to_sd\n", *(uint32_t *)dir.dir_d);
        lv_fs_dir_close(&dir);
        pfile = lv_mem_alloc(sizeof(ui_file_t));
        memset(pfile,0,sizeof(ui_file_t));
        ret = lv_fs_open(pfile,path,LV_FS_MODE_RD);
        printf("ret is %d in lv_fs_open\n",ret);
        if(LV_FS_RES_OK != ret) {
            printf("%s is not exist!\n",path);
            ret = lv_fs_close(pfile);
            memset(pfile,0,sizeof(ui_file_t));
            ret = lv_fs_open(pfile,path,LV_FS_MODE_WR);
            if(LV_FS_RES_OK != ret)
            {
                printf("%s create fail!\n",path);
                lv_mem_free(pfile);
                return;
            }
            ret = lv_fs_close(pfile);
            printf("ret is %d in lv_fs_close\n",ret);
        } else {
            printf("%s is exist!\n",path);
            ret = lv_fs_close(pfile);
        }
        memset(pfile,0,sizeof(ui_file_t));
        ret = lv_fs_open(pfile,path,LV_FS_MODE_RD | LV_FS_MODE_APPEND);
        printf("ret is %d in lv_fs_open\n",ret);
        if(LV_FS_RES_OK != ret) {
            printf("%s second open failed!\n",path);
            LV_ASSERT_MEM(pfile->file_d);
            lv_mem_free(pfile);
            return;
        }
        ret = lv_fs_seek(pfile, 0);
        printf("ret is %d in lv_fs_seek\n",ret);
        ret = lv_fs_write(pfile, preyuvBuf, write_size ,&ret_len);
        printf("ret is %d retlen is %d in lv_fs_write\n",ret,ret_len);
        if(0 == ret_len)
        {
            printf("write yuvBuf in sdfs is wrong!\n");
            lv_fs_close(pfile);
            lv_mem_free(pfile);
            return;
        }
        lv_fs_close(pfile);
        lv_mem_free(pfile);
    } else {
        printf("opendir %s failed in save_preview_yuv_to_sd!\n", dir_path);
        return;
    }
}
#endif

static void qrcode_scan_preview_task(lv_task_t * task)
{
    int result = 2;
    lv_obj_t * cam_obj = lv_watch_get_activity_obj(ACT_ID_QRCODE_SCAN);
    lv_obj_t * top_obj = lv_watch_get_top_activity_obj();
    if(top_obj != cam_obj) {
        if(Hal_Camera_Get_On_Flag()) {
            Hal_Camera_Stop_Preview(0);
        }
    } else {
        if(!Hal_Camera_Get_On_Flag()) {
            cam_start_preview_t cam_params;
            cam_params.width = 240;
            cam_params.height = 240;
            cam_params.sensor_id = 0;
            Hal_Camera_Start_Preview(&cam_params);
        }
        lv_img_cache_invalidate_src(NULL);
        lv_camera_obj_ext_t * ext = qrcode_scan_get_ext();
        lv_area_t cords;
        lv_obj_get_coords(ext->img, &cords);
        if(cords.x1 != 0) return;  //return if in sliding
        if(Hal_Camera_Preview((uint8_t *)img_camera.data)) {
            //update the src of img.
            lv_img_set_src(ext->img, &img_camera);
            if (yuvBuf) {
                lv_mem_free(yuvBuf);
                yuvBuf = NULL;
            }
            int pre_w = LV_HOR_RES;
            int pre_h = LV_VER_RES;
#if USE_CRANE_CAMERA_FULL_SIZE
            Hal_Camera_Preview_Yuv_Size(&pre_w,&pre_h);
#endif
#ifndef BUILD_IN_PC_SIMULATOR
#if USE_LV_WATCH_THIRD_LIB_QRCODE_SCAN == 0
            yuvBuf = lv_mem_alloc(pre_w * pre_h * 3/2);
            LV_ASSERT_MEM(yuvBuf);
            if(yuvBuf == NULL) return;
            memset((void *)yuvBuf, 0, pre_w * pre_h * 3/2);
            Hal_Camera_Preview_Qrcode_Scan((uint8_t *)yuvBuf);
            memset(yuvBuf + (pre_w*pre_h),128,(pre_w*pre_h/2));
#if YUV_DIRECT_CODE_ZXING
            result = qrcode_direct_decode(QRCODE_DECODER_ZXING, pre_w, pre_h, yuvBuf, qrcode_scan_decode_cb);
#else
            result = qrcode_direct_decode(QRCODE_DECODER_ZBAR, pre_w, pre_h, yuvBuf, qrcode_scan_decode_cb);
#endif
#else
            yuvBuf = lv_mem_alloc(pre_w * pre_h * 3/2);
            LV_ASSERT_MEM(yuvBuf);
            if(yuvBuf == NULL) return;
            memset((void *)yuvBuf, 0, pre_w * pre_h * 3/2);
            Hal_Camera_Preview_Qrcode_Scan((uint8_t *)yuvBuf);
            char parseStr[4000];
            memset(parseStr,0,4000);
            memset(yuvBuf + (pre_w*pre_h),128,(pre_w*pre_h/2));
            uint32_t start = lv_tick_get();
            if (count == 0)
            {
                printf("count is %d in qrcode_scan\n",count);
                result = qrcode_scan((char*)yuvBuf,
                        pre_w,
                        pre_h,
                        1,
                        parseStr,
                        4000);
                lv_mem_free(yuvBuf);
                yuvBuf = NULL;
                count=1;
            }
            else
            {
                uint8_t *pFront = NULL;
                uint8_t *pTail = NULL;
                int front_cnt = 0;
                int tial_cnt = 0;
                bool white_flg = true;
                int i = 0;
                int j = 0;
                int front_line = 0;
                int tail_line = 0;
                int up_line = 0;
                int down_line = 0;
                count++;
                for (i = 0;i < pre_h;i++)
                {
                    for(j = 0;j < 10;j++)
                    {
                        pFront = (uint8_t *)yuvBuf + (i * pre_w) + j;
                        if (*pFront < 25)
                        {
                            front_cnt++;
                        }
                        if(front_cnt == 10)
                        {
                            white_flg = false;
                        }
                        pTail = (uint8_t *)yuvBuf + (i * pre_w) + (pre_w - 10) + j;
                        if (*pTail < 25)
                        {
                            tial_cnt++;
                        }
                        if(tial_cnt == 10)
                        {
                            white_flg = false;
                        }
                        if (white_flg == false)
                            break;
                    }
                    if (white_flg == false)
                        break;
                }
                printf("white_flg is %d,front_cnt is %d and tial_cnt is %d in qrcode_scan\n",white_flg,front_cnt,tial_cnt);
                #if 1
                front_cnt = 0;
                tial_cnt = 0;
                if(white_flg == true)
                {
                    for (i = 0;i < 10;i++)
                    {
                        for(j = 0;j < pre_w;j++)
                        {
                            pFront = (uint8_t *)yuvBuf + (i * pre_w) + j;
                            if (*pFront < 25)
                            {
                                front_cnt++;
                            }
                            if(front_cnt == 10)
                            {
                                white_flg = false;
                            }
                            pTail = (uint8_t *)yuvBuf + ((i + (pre_w - 10)) * pre_w) + j;
                            if (*pTail < 25)
                            {
                                tial_cnt++;
                            }
                            if(tial_cnt == 10)
                            {
                                white_flg = false;
                            }
                            if (white_flg == false)
                                break;
                        }
                    }
                }
                printf("white_flg is %d,up_cnt is %d and down_cnt is %d in qrcode_scan\n",white_flg,front_cnt,tial_cnt);
                #endif
                if(white_flg == true)
                {
                    int front_sum_curr = 0;
                    int front_sum_prev = 0;
                    int tail_sum_curr = 0;
                    int tail_sum_prev = 0;
                    for(i = 10;i < (pre_w - 10);i++)
                    {
                        for(j = 10;j < (pre_h - 10);j++)
                        {
                            if(front_line == 0)
                            {
                                pFront = (uint8_t *)yuvBuf + i + (j * pre_w);
                                front_sum_curr += *pFront;
                            }
                            if(tail_line == 0)
                            {
                                pTail = (uint8_t *)yuvBuf + i + (((pre_h - 1) - j) * pre_w);
                                tail_sum_curr += *pTail;
                            }
                        }
                        printf("unok :front_sum_curr is %d and front_sum_prev is %d and i is %d\n",front_sum_curr,front_sum_prev,i);
                        printf("unok :tail_sum_curr is %d and tail_sum_prev is %d and 639 - i is %d\n",tail_sum_curr,tail_sum_prev,639 - i);
                        if((front_sum_curr <= front_sum_prev - 1000)&&(i != 10)&&(front_line == 0))
                        {
                            printf("front_sum_curr is %d and front_sum_prev is %d and front_line is %d\n",front_sum_curr,front_sum_prev,i - 10);
                            front_line = i - 10;
                        }
                        else
                        {
                            front_sum_prev = front_sum_curr;
                            front_sum_curr = 0;
                        }
                        if(( tail_sum_curr <= tail_sum_prev - 1000)&&(i != 10)&&(tail_line == 0))
                        {
                            printf("tail_sum_curr is %d and tail_sum_prev is %d and tail_line is %d\n",tail_sum_curr,tail_sum_prev,639 - i + 10);
                            tail_line = 639 - i + 10;
                        }
                        else
                        {
                            tail_sum_prev = tail_sum_curr;
                            tail_sum_curr = 0;
                        }
                        #if 1
                        if((tail_line != 0)&&(front_line != 0))
                        {
                            break;
                        }
                        #endif
                    }
                    #if 1
                    int up_descend_cnt = 0;
                    int up_interrupt_cnt = 0;
                    int down_descend_cnt = 0;
                    int down_interrupt_cnt = 0;

                    int up_sum_curr = 0;
                    int up_sum_prev = 0;
                    int down_sum_curr = 0;
                    int down_sum_prev = 0;
                    for(i = 10;i < (pre_h - 10);i++)
                    {
                        for(j = 10;j < (pre_w - 10);j++)
                        {
                            if(up_line == 0)
                            {
                                pFront = (uint8_t *)yuvBuf + (i * pre_w) + j;
                                up_sum_curr += *pFront;
                            }
                            if(down_line == 0)
                            {
                                pTail = (uint8_t *)yuvBuf + (((pre_h - 1) - i) * pre_w) + j;
                                down_sum_curr += *pTail;
                            }
                        }
                        printf(" unok :up_sum_curr is %d and up_sum_prev is %d and i is %d\n",up_sum_curr,up_sum_prev,i);
                        printf(" unok :down_sum_curr is %d and down_sum_prev is %d and 479 - i is %d\n",down_sum_curr,down_sum_prev,479 - i);
                        if((up_sum_curr <= up_sum_prev)&&(up_line == 0))
                        {
                            up_descend_cnt++;
                            printf("up_sum_curr is %d and up_sum_prev is %d and up_line is %d and up_descend_cnt is %d\n",up_sum_curr,up_sum_prev,i,up_descend_cnt);
                            if (up_descend_cnt == 8)
                            {
                                up_line = i - 10;
                            }
                        }
                        else
                        {
                            if(up_line == 0)
                            {
                                up_interrupt_cnt++;
                                printf("up_sum_curr is %d and up_sum_prev is %d and up_line is %d and up_interrupt_cnt is %d\n",up_sum_curr,up_sum_prev,i,up_interrupt_cnt);
                                if(up_interrupt_cnt == 3)
                                {
                                    up_descend_cnt = 0;
                                    up_interrupt_cnt = 0;
                                }
                            }
                        }
                        if(( down_sum_curr <= down_sum_prev)&&(down_line == 0))
                        {
                            down_descend_cnt++;
                            printf("down_sum_curr is %d and down_sum_prev is %d and down_line is %d and down_descend_cnt is %d\n",down_sum_curr,down_sum_prev,479 - i,down_descend_cnt);
                            if (down_descend_cnt == 8)
                            {
                                down_line = (pre_h - 1) - i + 10;
                            }
                        }
                        else
                        {
                            if(down_line == 0)
                            {
                                down_interrupt_cnt++;
                                printf("down_sum_curr is %d and down_sum_prev is %d and down_line is %d and down_interrupt_cnt is %d\n",down_sum_curr,down_sum_prev,479 - i,down_interrupt_cnt);
                                if(down_interrupt_cnt == 3)
                                {
                                    down_descend_cnt = 0;
                                    down_interrupt_cnt = 0;
                                }
                            }
                        }
                        up_sum_prev = up_sum_curr;
                        up_sum_curr = 0;
                        down_sum_prev = down_sum_curr;
                        down_sum_curr = 0;
                        #if 1
                        if((down_line != 0)&&(up_line != 0))
                        {
                            break;
                        }
                        #endif
                    }
                    #endif
                    printf("up_line is %d,down_line is %d and front_line is %d, tail_line is %d in qrcode_scan\n",up_line,down_line,front_line,tail_line);
                    if((up_line == 0)||(down_line == 0)||(front_line == 0)||(tail_line == 0))
                    {
                        white_flg = false;
                    }
                    else
                    {
                        float r = (float)(down_line -up_line + 1)/(tail_line - front_line + 1) * 100;
                        printf("ver and hor percent is %.2f in qrcode_scan\n",r);
                        if((r >= 80)&&(r <= 120))
                        {
                            int ret;
                            uint8_t *pDst_qr = (uint8_t *)lv_mem_aligned_alloc(1000*750*3/2+32, 32);
                            if (pDst_qr == NULL) {
                                printf("Failed:  pDst_qr qrcode_scan_preview_task no enough memory!!!\n");
                                return;
                            }

                            ret = yuv420_scale(pre_w, pre_h, 1000, 750,
                                               (uint8_t *)yuvBuf, pDst_qr, 1);
                            lv_mem_free(yuvBuf);
                            yuvBuf = NULL;
                            if (ret == 1 ) {
                                result = qrcode_scan((char*)pDst_qr,
                                            1000,
                                            750,
                                            1,
                                            parseStr,
                                            4000);
                            }
                            printf("count is %d in qrcode_scan\n",count);
                            printf("it is a qrcode in qrcode_scan\n");
                            lv_mem_aligned_free(pDst_qr);
                            count = 0;
                        }
                        else if(r < 80)
                        {
                            printf("count is %d in qrcode_scan\n",count);
                            printf("it is a barcode in qrcode_scan\n");
                            result = qrcode_scan((char*)yuvBuf,
                                    pre_w,
                                    pre_h,
                                    1,
                                    parseStr,
                                    4000);
                            lv_mem_free(yuvBuf);
                            yuvBuf = NULL;
                            count = 0;
                        }
                    }
                }
                else
                {
                    printf("white_flg is %d in qrcode_scan\n",white_flg);
                }
                if(QRCODE_SCALE_TRY_COUNT + 1 == count)
                {
                    count = 0;
                }
            }
            printf("cost time %d in qrcode_scan\n",lv_tick_elaps(start));
            if(0 == result)
            {
                qrcode_scan_decode_cb(parseStr);
                count = 0;
            }
            printf("result is %u in USE_LV_WATCH_THIRD_LIB_QRCODE_SCAN in %s\n",result,__FUNCTION__);
#endif
#endif
            printf("result is %u in %s\n",result,__FUNCTION__);
        }
        if(preview_task)
            lv_task_set_period(preview_task, 100);
    }

}

static void qrscan_stop_camera_task(lv_task_t * task)
{
    Hal_Camera_Stop_Preview(0);
}

static void qrcode_scan_prepare_destory(lv_obj_t * activity_obj)
{
    if(Hal_Camera_Get_On_Flag()) {
        lv_task_t * destroy_task = lv_task_create(qrscan_stop_camera_task, 50, LV_TASK_PRIO_HIGH, NULL);
        lv_task_once(destroy_task);
    }
    lv_watch_png_cache_all_free();
    lv_mem_free(img_camera.data);
    img_camera.data = NULL;
    if(NULL != yuvBuf) {
        lv_mem_free(yuvBuf);
        yuvBuf = NULL;
    }
    if(preview_task) {
        lv_task_del(preview_task);
        preview_task = NULL;
    }
#ifndef BUILD_IN_PC_SIMULATOR
#if USE_LV_WATCH_THIRD_LIB_QRCODE_SCAN != 0
    count = 0;
#endif
#endif
}

#endif /*USE_LV_WATCH_QRCODE_SCAN*/
